#include "StdAfx.h"
#include "SocketEngine.h"

SocketEngine::SocketEngine(void)
{
    m_bIsConnected = false;
}

SocketEngine::~SocketEngine(void)
{
}

int SocketEngine::SeverSession()
{
    int nRtn(0);
    sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(DEFAULTPORT);
    sin.sin_addr.S_un.S_addr = INADDR_ANY;
    do 
    {
        if (::bind(m_socket, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)
        {
            ::WSACleanup();
            break;
        }
        if (::listen(m_socket, 2) == SOCKET_ERROR)
        {
            ::WSACleanup();
            break;
        }
        CloseHandle(CreateThread(0,0,(__stdcall)WaitMessageThread,0, 0));
    } while (0);
    nRtn = GetLastError();

	return nRtn;
}

int SocketEngine::ClientSession()
{
    int nRtn(0);
	// WSAStartup()
	// socket()
	// connect()
	// send() / recv()
	// closesocket()
	return nRtn;
}

int SocketEngine::Initlialize()
{
    int nRtn(0);
    WSADATA wsaData;
    WORD socketVersion = MAKEWORD(2,0);
    ::WSAStartup(socketVersion, &wsaData);

    m_socket = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (INVALID_SOCKET == s)
    {
        ::WSACleanup();
        nRtn = GetLastError();
    }
    return nRtn;
}

int SocketEngine::ReleaseSocket()
{
    int nRtn(0);
    return nRtn;
}

int SocketEngine::WaitMessageThread()
{

    return 0;
}